<?php

namespace app\api\controller;

use app\admin\model\product\Attr;
use app\admin\model\product\Attrs;
use app\admin\model\product\Collect;
use app\admin\model\product\Lists;
use app\admin\model\product\Sku;
use app\admin\model\product\Value;
use app\common\controller\Api;
use think\Request;


class Productlists extends Api
{
    private static $model;
    protected $noNeedLogin = ['listsByField', 'read', 'sku'];
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new Lists();
    }

    /**
     * 显示资源列表
     * @throws \think\exception\DbException
     */
    public function listsByField()
    {
        $pageSize = input('page_size', 15);

        $where = [
            'status' => 'normal',
        ];
        $categoryId = input('category_id');
        if ($categoryId) {
            $where['category_id'] = $categoryId;
        }

        $keyword = trim(input('keyword'));
        if ($keyword) {
            $where['name'] = ['like', '%' . $keyword . '%'];
        }

        $list = self::$model->field('id,name,image,intro,detail,stock')->where($where)->order('id desc')->paginate($pageSize)->each(function ($item) {
            $item['image'] = make_full_url($item['image']);
        });
        $this->success('物资列表', $list);
    }

    /**
     * 商品详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read()
    {
        $id = $this->request->get('id');
        if (!$id) {
            $this->error('参数缺失！');
        }

        $row = self::$model->find($id);
        if (!$row) {
            $this->error('该商品不存在！');
        }

        $row['image'] = make_full_url($row['image']);
        $this->success('物品详情', $row);
    }


    /**
     * 是否收藏商品
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function isCollect()
    {
        $product_id = $this->request->post('product_id');
        $id = $this->auth->id;
        $row = Collect::where('product_id', $product_id)->where('user_id', $id)->find();

        if ($row) {
            $this->success('已收藏该商品', true);
        } else {
            $this->success('未收藏该商品', false);
        }
    }

    /**
     * 收藏/取消
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function changeCollect()
    {
        $product_id = $this->request->post('product_id');
        $id = $this->auth->id;
        $row = Collect::where('product_id', $product_id)->where('user_id', $id)->find();
        if ($row) {
            Collect::where('id', $row->id)->delete();
            $this->success('取消成功');
        } else {
            Collect::create([
                'user_id' => $id,
                'product_id' => $product_id,
            ]);
            $this->success('收藏成功');
        }
    }
}
